# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json



class ScrapyCarPipeline(object):

    def process_item(self, item, spider):
        if item.class_name!='car':
            return item
        type=item['type']
        if type.find('MPV') != -1:
            type='MPV'
        elif type.find('SUV')!=-1:
            type='SUV'
        else:
            type=type.split()[0]
        item['type']=type
        brand_national_map = {}
        with open('add_national.json', 'r', encoding='utf-8') as fp:
            k = json.load(fp)
            for el in k:
                brand_national_map[el['brand']] = el['national']
        national=brand_national_map.get(item['brand'],'中国')
        item['national']=national
        print(item)
        return item